#!/bin/sh

#-------------------------------------#
#  SGT (Software Generation Toolkit)  #
#-------------------------------------#
#                                     #
#     Create the current library      #
#   with dependencies, compilation    #
#    link and binary construction     #
#                                     #
#  GDE 13/10/2003 v3.3                #
#       Add -nzip option              #
#                                     #
#  GDE 03/07/2003 v3.2                #
#       check \MAK subdirectory       #
#       Add -ne2p option              #
#       Add -ncsn option              #
#       Add -dwl option               #
#                                     #
#  GDE 22/04/2003 v3.1                #
#       Update comments               #
#                                     #
#  GDE 06/12/2002 v3.0                #
#       add -bin argument             #
#       update argument process       #
#                                     #
#  GDE 07/05/2002 v2.1                #
#       update for SGT 1.1.0          #
#                                     #
#  GDE 22/03/2002 v2.0                #
#       Disable dependencies          #
#       by default                    #
#                                     #
#  GDE 20/12/2001 v1.0                #
#       creation                      #
#                                     #
#-------------------------------------#
# $1: target makefile                 #
# $2: other options                   #
# $3-9: other options/specific rules  #
#-------------------------------------#



#-------------------------------------#
# Script informations
#-------------------------------------#
export SCRIPT_SGT_VER=3.2
export SCRIPT_SGT_DATE=03/07/2003
export SCRIPT_SGT_USER=GDE
export SCRIPT_SGT_NAME=make_lib
export SCRIPT_SGT_USAGE="Launch a full library or binary process"
#-------------------------------------#



#---------------------
# Create and go to output directory
#---------------------
if [ -d mak ]
then
	mkdir -p -m775 out
	cd out
else
	export MSG_ERROR_SCRIPT=0
	export CMD_SCRIPT=Error
fi
#---------------------



#-------------------------------------#
# Parameters section
#-------------------------------------#


# Default value :
export OPTION_MAK1=
export OPTION_MAK2=
export OPTION_MAK3=
export OPTION_MAK4=
export OPTION_MAK5=
export OPTION_MAK6=
export OPTION_MAK7=
export RULE_MAK=

#---------------------
# Parameter 8 -> other options
#---------------------
if [ ! -z $8 ] && [ -z $CMD_SCRIPT ]
then
	if [ $8 = -w ]
	then
		export OPTION_MAK6=WARNING=OFF
	else
		if [ $8 = -d ]
		then
			export OPTION_MAK6=DEPENDENCIES=ON
		else
			if [ $8 = -bin ]
			then
				export OPTION_MAK6=MAKE_PRO64K=OFF
			else
				if [ $8 = -dwl ]
				then
					export OPTION_MAK6=MAKE_DWL_FROM_TARGET=ON
				else
					if [ $8 = -ne2p ]
					then
						export OPTION_MAK6=MANAGE_E2P=OFF
					else
						if [ $8 = -ncsn ]
						then
							export OPTION_MAK6=SRC_CSN_LIST=
						else
							if [ $8 = -nzip ]
							then
								export OPTION_MAK7=ZIP_AXF_FILE=OFF
							else
								export RULE_MAK=$8
							fi
						fi
					fi
				fi
			fi
		fi
	fi
fi

#---------------------
# Parameter 7 -> other options
#---------------------
if [ ! -z $7 ] && [ -z $CMD_SCRIPT ]
then
	if [ $7 = -w ]
	then
		export OPTION_MAK6=WARNING=OFF
	else
		if [ $7 = -d ]
		then
			export OPTION_MAK6=DEPENDENCIES=ON
		else
			if [ $7 = -bin ]
			then
				export OPTION_MAK6=MAKE_PRO64K=OFF
			else
				if [ $7 = -dwl ]
				then
					export OPTION_MAK6=MAKE_DWL_FROM_TARGET=ON
				else
					if [ $7 = -ne2p ]
					then
						export OPTION_MAK6=MANAGE_E2P=OFF
					else
						if [ $7 = -ncsn ]
						then
							export OPTION_MAK6=SRC_CSN_LIST=
						else
							if [ $7 = -nzip ]
							then
								export OPTION_MAK6=ZIP_AXF_FILE=OFF
							else
								export RULE_MAK=$7
							fi
						fi
					fi
				fi
			fi
		fi
	fi
fi

#---------------------
# Parameter 6 -> other options
#---------------------
if [ ! -z $6 ] && [ -z $CMD_SCRIPT ]
then
	if [ $6 = -w ]
	then
		export OPTION_MAK5=WARNING=OFF
	else
		if [ $6 = -d ]
		then
			export OPTION_MAK5=DEPENDENCIES=ON
		else
			if [ $6 = -bin ]
			then
				export OPTION_MAK5=MAKE_PRO64K=OFF
			else
				if [ $6 = -dwl ]
				then
					export OPTION_MAK5=MAKE_DWL_FROM_TARGET=ON
				else
					if [ $6 = -ne2p ]
					then
						export OPTION_MAK5=MANAGE_E2P=OFF
					else
						if [ $6 = -ncsn ]
						then
							export OPTION_MAK5=SRC_CSN_LIST=
						else
							if [ $6 = -nzip ]
							then
								export OPTION_MAK5=ZIP_AXF_FILE=OFF
							else
								export RULE_MAK=$6
							fi
						fi
					fi
				fi
			fi
		fi
	fi
fi

#---------------------
# Parameter 5 -> other options
#---------------------
if [ ! -z $5 ] && [ -z $CMD_SCRIPT ]
then
	if [ $5 = -w ]
	then
		export OPTION_MAK4=WARNING=OFF
	else
		if [ $5 = -d ]
		then
			export OPTION_MAK4=DEPENDENCIES=ON
		else
			if [ $5 = -bin ]
			then
				export OPTION_MAK4=MAKE_PRO64K=OFF
			else
				if [ $5 = -dwl ]
				then
					export OPTION_MAK4=MAKE_DWL_FROM_TARGET=ON
				else
					if [ $5 = -ne2p ]
					then
						export OPTION_MAK4=MANAGE_E2P=OFF
					else
						if [ $5 = -ncsn ]
						then
							export OPTION_MAK4=SRC_CSN_LIST=
						else
							if [ $5 = -nzip ]
							then
								export OPTION_MAK4=ZIP_AXF_FILE=OFF
							else
								export RULE_MAK=$5
							fi
						fi
					fi
				fi
			fi
		fi
	fi
fi

#---------------------
# Parameter 4 -> other options
#---------------------
if [ ! -z $4 ] && [ -z $CMD_SCRIPT ]
then
	if [ $4 = -w ]
	then
		export OPTION_MAK3=WARNING=OFF
	else
		if [ $4 = -d ]
		then
			export OPTION_MAK3=DEPENDENCIES=ON
		else
			if [ $4 = -bin ]
			then
				export OPTION_MAK3=MAKE_PRO64K=OFF
			else
				if [ $4 = -dwl ]
				then
					export OPTION_MAK3=MAKE_DWL_FROM_TARGET=ON
				else
					if [ $4 = -ne2p ]
					then
						export OPTION_MAK3=MANAGE_E2P=OFF
					else
						if [ $4 = -ncsn ]
						then
							export OPTION_MAK3=SRC_CSN_LIST=
						else
							if [ $4 = -nzip ]
							then
								export OPTION_MAK3=ZIP_AXF_FILE=OFF
							else
								export RULE_MAK=$4
							fi
						fi
					fi
				fi
			fi
		fi
	fi
fi

#---------------------
# Parameter 3 -> other options
#---------------------
if [ ! -z $3 ] && [ -z $CMD_SCRIPT ]
then
	if [ $3 = -w ]
	then
		export OPTION_MAK2=WARNING=OFF
	else
		if [ $3 = -d ]
		then
			export OPTION_MAK2=DEPENDENCIES=ON
		else
			if [ $3 = -bin ]
			then
				export OPTION_MAK2=MAKE_PRO64K=OFF
			else
				if [ $3 = -dwl ]
				then
					export OPTION_MAK2=MAKE_DWL_FROM_TARGET=ON
				else
					if [ $3 = -ne2p ]
					then
						export OPTION_MAK2=MANAGE_E2P=OFF
					else
						if [ $3 = -ncsn ]
						then
							export OPTION_MAK2=SRC_CSN_LIST=
						else
							if [ $3 = -nzip ]
							then
								export OPTION_MAK2=ZIP_AXF_FILE=OFF
							else
								export RULE_MAK=$3
							fi
						fi
					fi
				fi
			fi
		fi
	fi
fi

#---------------------
# Parameter 2 -> other options
#---------------------
if [ ! -z $2 ] && [ -z $CMD_SCRIPT ]
then
	if [ $2 = -w ]
	then
		export OPTION_MAK1=WARNING=OFF
	else
		if [ $2 = -d ]
		then
			export OPTION_MAK1=DEPENDENCIES=ON
		else
			if [ $2 = -bin ]
			then
				export OPTION_MAK1=MAKE_PRO64K=OFF
			else
				if [ $2 = -dwl ]
				then
					export OPTION_MAK1=MAKE_DWL_FROM_TARGET=ON
				else
					if [ $2 = -ne2p ]
					then
						export OPTION_MAK1=MANAGE_E2P=OFF
					else
						if [ $2 = -ncsn ]
						then
							export OPTION_MAK1=SRC_CSN_LIST=
						else
							if [ $2 = -nzip ]
							then
								export OPTION_MAK1=ZIP_AXF_FILE=OFF
							else
								export RULE_MAK=$2
							fi
						fi
					fi
				fi
			fi
		fi
	fi
fi

#---------------------
# Parameter 1 -> target makefile
#---------------------
if [ ! -z $1 ] && [ -z $CMD_SCRIPT ]
then
	if [ $1 = -v ]
	then
		export CMD_SCRIPT=Version
	else
		if [ $1 = -h ]
		then
			export CMD_SCRIPT=Help
		else
			if [ $1 = --help ]
			then
				export CMD_SCRIPT=Help
			else
				if [ $1 = -hm ]
				then
					export CMD_SCRIPT=More
				else
					if [ $1 = -u ]
					then
						export CMD_SCRIPT=Usage
					else
						if [ -f ../mak/$1.mak ]
						then
							export TARGET_LIB=$1
						else
							export MSG_ERROR_SCRIPT=1
							export CMD_SCRIPT=Error
						fi
					fi
				fi
			fi
		fi
	fi
fi


#-------------------------------------#
# if no option is defined, display help
#-------------------------------------#
if [ $# -eq 0 ] && [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Help
fi


#-------------------------------------#
# Process section
#-------------------------------------#
#---------------------
# Launch compilation
#---------------------
if [ ! -z $TARGET_LIB ] && [ -z $CMD_SCRIPT ]
then
	echo " "
	echo "    $SCRIPT_SGT_USAGE"
	echo " "
	make -f ../mak/gen.mak TARGET=$TARGET_LIB $OPTION_MAK1 $OPTION_MAK2 $OPTION_MAK3 $OPTION_MAK4 $OPTION_MAK5 $OPTION_MAK6 $RULE_MAK
	exit
fi



#-------------------------------------#
# Help script section
#-------------------------------------#
if [ -z $CMD_SCRIPT ]
then
	export CMD_SCRIPT=Version
fi

#---------------------
# Usage
#---------------------
if [ $CMD_SCRIPT = Usage ]
then
	echo "  $SCRIPT_SGT_NAME	$SCRIPT_SGT_USAGE"
	exit
fi

#---------------------
# Version
#---------------------
echo " "
echo "-------------------------------------"
echo " '$SCRIPT_SGT_NAME' (release "$SCRIPT_SGT_VER") for SGT "$SGT_VER". Last update by "$SCRIPT_SGT_USER" on "$SCRIPT_SGT_DATE
echo "  $SCRIPT_SGT_USAGE"
if [ $CMD_SCRIPT = Version ]
then
	exit
fi

#---------------------
# Simple help
#---------------------
if [ $CMD_SCRIPT = Help ]
then
	echo "-------------------------------------"
	echo " $SCRIPT_SGT_NAME [TARGET] [-OPTIONS] [RULE]"
	echo " "
	echo "  -d    Enable the dependencies for the current library"
	echo "  -w    Disable some warning levels"
	echo " "
	echo "  -bin  Create only the first binary file 'TARGET.bin' when linking"
	echo "  -dwl  Generate a binary file in X-Modem from 'TARGET.bin'"
	echo "  -ne2p Do not launch E2P management."
	echo "  -ncsn Do not manage CSN compilation."
	echo "  -nzip Do not archive AXF files."
	echo " "
	echo "-------------------------------------"
	echo "  -h | --help  Display this help "
	echo "  -hm          Display more help about SGT" $SGT_VER
	echo "  -v           Display the version of the current script"
fi

#---------------------
# More help
#---------------------
if [ $CMD_SCRIPT = More ]
then
	make -f ../mak/gen.mak help USE_TARGET_FILE=OFF
fi



#-------------------------------------#
# Error messages section
#-------------------------------------#
if [ $CMD_SCRIPT = Error ]
then
	if [ $MSG_ERROR_SCRIPT = 0 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : The root path is not a valid SGT directory. Please check for the \MAK sub-directory"
	fi
	if [ $MSG_ERROR_SCRIPT = 1 ]
	then
		echo "-------------------------------------"
		echo " "
		echo "ERROR : $1 or ../mak/$1.mak doesn't exist !"
	fi
	echo " "
	echo " Check your command line or see help section -> $SCRIPT_SGT_NAME -h"
fi